<?php
namespace Wechat\Controller;

class DingController extends BaseController{
    /*已付款订单首页*/
    public function index()
    {
        $count      = M('ding')->where(['where'=>1])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,30);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('ding')->where(['where'=>1])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        /*0为未付款 1为已付款代发货 3为已发货 4为交易成功*/
        foreach ($list as $k => $v){
            $list[$k]['goodid'] = M('goods')->where(['id' => $v['goodid']])->find();
            $list[$k]['dizhi'] = M('add')->where(['id' => $v['dizhi']])->find();
        }
        /*
        echo '<pre/>';
        var_dump($list);
        die();
        */
        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*确认发货*/
    public function send()
    {
        $id = i('get.id');
        /*订单数据*/
        $dingdan = M('ding')->where(['id'=>$id])->find();

        /*商品信息*/
        $goods = M('goods')->where(['id'=>$dingdan['goodid']])->find();
        /*收货人信息*/
        $human = M('add')->where(['id'=>$dingdan['dizhi']])->find();

        $post = i('post.');
        $data = [
            'did'=>$id,//订单id
            'name'=>$post['name'],//快递运营商
            'number'=>$post['number'],//快递单号
            'goodname'=>$goods['goods_name'],//商品名称
            'time'=>time(),//发货时间
            'shouhuo'=>$human['add_name'],//收货人姓名
            'city'=>$human['add_city'].$human['xiangxi'],//收货人地址
            'tel'=>$human['add_tel'],//收货人电话
        ];
        /*订单更改状态*/
        $data1 = [
            'where'=>2,
        ];
        if(IS_POST){
            if(trim(empty($post['number']))){
                $this->error('请填写单号信息');
            }
            if(M('kuaidi')->add($data)){
                M('ding')->where(['id'=>$id])->save($data1);
                $this->success('提交成功',U('Wechat/Ding/index'));
            }else{
                $this->error('提交失败，未知错误');
            }
        }else{
            $this->display();
        }
    }
    /*已发货*/
    public function ok()
    {
        $count      = M('kuaidi')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,30);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('kuaidi')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        /*0为未付款 1为已付款代发货 2为已发货 3为交易成功*/
        foreach ($list as $k => $v){
            $list[$k]['did'] = M('ding')->where(['id' => $v['did']])->find();
        }
        /*
        echo '<pre/>';
        var_dump($list);
        die();
        */
        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*交易成功*/
    public function okok()
    {
        $id = i('get.id');
        $data = [
            'where'=>3,
        ];
        if(M('ding')->where(['id'=>$id])->save($data)){
            $this->success('确认成功');
        }else{
            $this->error('失败，未知错误');
        }
    }
    /*删除记录*/
    public function shanchu()
    {
        /*快递记录*/
        $kuaidi = i('get.id');
        /*订单记录*/
        $dingdan = i('get.dingid');

        if(M('ding')->where(['id'=>$dingdan])->delete()){
            M('kuaidi')->where(['id'=>$kuaidi])->delete();
            $this->success('删除成功');
        }else{
            $this->error('删除失败,未知错误');
        }
    }
}